package org.opens.javaskill.design;

/**
 * 功能:
 * <p>装态模式的demo</p>
 * @Date 2020/8/29 13:37
 * @Created by 99126
 */
public class StatePattern {

    private static ThatState startState = new StartState(1);
    private static ThatState stopState = new StartState(2);

    public static void main(String[] args) {
        StateContext context = new StateContext();

        System.out.println(context.getState());
        startState.doAction(context);
        System.out.println(context.getState());
        stopState.doAction(context);
        System.out.println(context.getState());
    }

}

/**
 * 说明:
 * <pre>
 *     1. 这个类就是要被修改的对象;
 * </pre>
 */
class StateContext {

    private ThatState state;

    public void setState(ThatState state) {
        this.state = state;
    }

    public ThatState getState() {
        return state;
    }

    @Override
    public String toString() {
        return "StateContext{" +
                "state=" + state +
                '}';
    }
}

/**
 * 说明:
 * <pre>
 *     1. 这个类是所有状态的父类;
 * </pre>
 */
abstract class ThatState {

    private int value;

    public ThatState(int value) {
        this.value = value;
    }

    /**
     * 功能:
     * <p>抽象状态变化.</p>
     */
    public abstract void doAction(StateContext stateContext);

    @Override
    public String toString() {
        return "ThatState{" +
                "value=" + value +
                '}';
    }
}

/**
 * 说明:
 * <pre>
 *     1. 这个类是start状态;
 * </pre>
 */
class StartState extends ThatState {

    public StartState(int value) {
        super(value);
    }

    @Override
    public void doAction(StateContext stateContext) {
        stateContext.setState(this);
    }

}

/**
 * 说明:
 * <pre>
 *     1. 这个类是stop状态;
 * </pre>
 */
class StopState extends ThatState {

    public StopState(int value) {
        super(value);
    }

    @Override
    public void doAction(StateContext stateContext) {
        stateContext.setState(this);
    }

}